<?php	
	
	$info = ma('SELECT * FROM `'.$dbprefix.'categories` WHERE `id`='.$id_category.';');
	$id_datatype = $info['id_datatype'];
	echo '<h1>'.$info['name'].'</h1><p>'.$info['description'].'</p>';
	echo '<form method="post" action="index.php?cat='.$id_category.'">';
	
	if(!empty($_POST)) {		
		if(isset($_POST['add']) || isset($_POST['edit'])) {
			// Add or edit an item
			$action = isset($_POST['add']) ? 'add' : 'edit';
			if($action=='add') {
				// Add
				echo '<h2>Add new item</h2>';				
				$info = array('name'=>'', 'id_category'=>0);
			} elseif($action=='edit') {
				// Edit
				echo '<h2>Edit item</h2>';
				if(isset($_POST['id'])) {
					$id = $_POST['id'];
					echo '<input type="hidden" name="id" value="'.$id.'" />';				
					$info = ma('SELECT * FROM `'.$dbprefix.'items` WHERE `id`='.$id);
				} else {
					// If there is no ID sent, this cannot be an edit. So it is an add.
				$info = array('name'=>'', 'id_category'=>0);
					$action = 'add';
				}
			}
			echo '<input type="hidden" name="action" value="'.$action.'" />';
			echo '<input type="hidden" name="id_category" value="'.$id_category.'" />';
			?>
				<div>
					<label>Name:</label>
					<input class="text" type="text" name="name" value="<?php echo $info['name']; ?>" />
				</div>
				<h3>Options</h3>
			<?php
			// Build the form:
			$result = mq('SELECT A.*, B.`hasmany` FROM `'.$dbprefix.'options` A, `'.$dbprefix.'datatype_option` B WHERE B.`id_datatype`='.$id_datatype.' AND B.`id_option` = A.`id`;');
			while($option = ma($result)) {
				if(empty($option['description'])) { $option['description']=$option['name']; }
				if($action=='edit' && $option['type']!='category') {
					$option['value'] = sr('SELECT `value` FROM `'.$dbprefix.'values` WHERE `id_option`='.$option['id'].' AND `id_item`='.$info['id'].';');
				}
				
				echo $option['hasmany']==0 ? '<div>' : '<div class="hasmany">';
				echo '<label>'.$option['description'].':</label>';
				if($option['hasmany']==1) { echo '<ol><li>'; }
				switch($option['type']) {
					case 'textarea':
						{
							// Textarea
							echo '<textarea rows="6" cols="40" name="option_'.$option['id'].'">'.$option['value'].'</textarea>';
							break;
						}
					case 'category' :
						{							
							// Category
							// Show the contents of this category:
							$sql = 'SELECT `id`,`name` FROM `'.$dbprefix.'items` WHERE `id_category`='.$option['value'].' ORDER BY `name` ASC;';
							$categoryResult = mq($sql);
							if(mysql_num_rows($categoryResult)==0) {
								echo '<select name="option_'.$option['id'].'" disabled="disabled"><option value="0">Category is empty</option></select>';
							} else {
								echo '<select name="option_'.$option['id'].'">';
								while($category = ma($categoryResult)) {
									echo '<option value="'.$category['id'].'"';
									if($category['id']==$option['value']) { echo ' selected="selected"'; }
									echo '>'.$category['name'].'</option>';
								}
								echo '</select>';
							}
							break;
						}
					case 'text' :
					case 'url' :
					case 'email':
					default:
						{
							// Default text input
							echo '<input class="text" type="text" name="option_'.$option['id'].'" value="'.$option['value'].'" />';
							break;
						}
				}
				if($option['hasmany']==1) {
					echo ' <a href="#" class="addAnother">Add another<var class="type">'.$option['type'].'</var><var class="optionID">'.$option['id'].'</var></a></li></ol>';
				}
				echo '</div>';
			}
			echo '<label>&nbsp;</label>';
			echo '<input type="submit" value="Save" name="save" />';				
		} elseif(isset($_POST['save'])) {
			// Save this item
			// Get the variables:
			$variables = array();
			$result = mq('SELECT A.* FROM `'.$dbprefix.'options` A, `'.$dbprefix.'datatype_option` B WHERE B.`id_datatype`='.$id_datatype.' AND B.`id_option` = A.`id`;');
			while($option = ma($result)) {
				array_push($variables, array('id'=>$option['id'], 'value'=>makeSafe($_POST['option_'.$option['id']])));				
			}
			$name        = makeSafe($_POST['name']);
			$id_category = makeSafe($_POST['id_category']);
			$action      = makeSafe($_POST['action']);
			if($action=='add') {
				mq('INSERT INTO `'.$dbprefix.'items` (`name`, `id_category`) VALUES (\''.$name.'\', '.$id_category.');');
				$id_item = sr('SELECT `id` FROM `'.$dbprefix.'items` WHERE `name`=\''.$name.'\' AND `id_category`='.$id_category.' ORDER BY `id` DESC;');
			} elseif($action=='edit') {
				$id_item = makeSafe($_POST['id']);
				mq('UPDATE `'.$dbprefix.'items` SET `name`=\''.$name.'\' WHERE `id`='.$id_item.';');
				// Delete all previously stored items before saving them again:
				mq('DELETE FROM `'.$dbprefix.'values` WHERE `id_item`='.$id_item.';');
			}
			// Store the variables:
			foreach($variables as $variable) {
				mq('INSERT INTO `'.$dbprefix.'values` (`id_item`, `id_option`, `value`) VALUES ('.$id_item.', '.$variable['id'].', \''.$variable['value'].'\');');				
			}
		} elseif(isset($_POST['delete'])) {
			// Delete this item
			$id_item = makeSafe($_POST['id']);
			mq('DELETE FROM `'.$dbprefix.'items` WHERE `id`='.$id_item.';');
			mq('DELETE FROM `'.$dbprefix.'values` WHERE `id_item`='.$id_item.';');
		}
	} else {
	
	// Show this category:
	if(sr('SELECT COUNT(*) AS `total` FROM `'.$dbprefix.'items` WHERE `id_category`='.$id_category.';')>0) {
?>

<table class="zebra optionlist">
	<tr>
		<th>&nbsp;</th>		
		<th class="wide">Name</th>
	</tr>	
<?php
	$result = mq('SELECT * FROM `'.$dbprefix.'items` WHERE `id_category`='.$id_category.';');
	while($info = ma($result)) {
		echo '<tr><td><input type="radio" name="id" value="'.$info['id'].'" /></td>';
		echo '<td class="expandable"><span class="expand"><span class="arrow">+</span><var class="id">'.$info['id'].'</var></span>'.$info['name'].'</td>';
		echo '</tr>';
	}
?>
</table>

<?php
	} else {
		echo '<p><em>This category is empty</em></p>';
	}
?>
	<input type="submit" name="edit" value="edit..." />
	<input type="submit" name="delete" value="delete" />
	<input type="submit" name="add" value="add new..." />	
<?php
	}
?>
</form>
